<?php
require("e/class/connect.php");
require("e/class/db_sql.php");
require('e/data/dbcache/class.php');
include("e/extend/wechat/wechat.config.php");
include("e/extend/wechat/wechat.class.php");
require("e/extend/wechat/wechat.fun.php");
$link=db_connect();
$empire=new mysqlquery();
$WeiXin=new Wechat($options);

$enews=$_POST['enews'];
if(empty($enews))
{$enews=$_GET['enews'];}
if($enews)
{
	if($enews=='sendTemplateMessage') //发送模板消息
	{
		$input = file_get_contents("php://input");
		$data  = json_decode($input,true);
		$res=$WeiXin->sendTemplateMessage($data);
	}
	else if($enews=='addTemplateMessage') //设置模板
	{
		$res = $WeiXin->addTemplateMessage($_GET['template_id']);
	}
	else if($enews=='delTemplateMessage') //删除模板
	{
		$res = $WeiXin->delTemplateMessage($_GET['template_id']);
	}
	else if($enews=='getTemplateAllList') //获取模板列表
	{
		//http://www.xxxxxx.com/api.php?operation=getTemplateAllList
		$res = $WeiXin->getTemplateAllList();
	}
	else if($enews=='getUserInfo') //获取用户信息
	{
		$openid=$_GET['openid']?$_GET['openid']:$_POST['openid'];
		$res=$WeiXin->getUserInfo($openid);
		if($res!==false) {
			$res['errcode']=0;
			$res['errmsg']='ok';
		}
	}
	else if($enews=='getUsersInfo') //批量获取用户信息
	{
		$openids=$_POST['openids'];
		if(count($openids)==0)
		{
			$res=array('errcode'=>40012,'errmsg'=>'openids 不能为空');
		}
		else
		{
			$arr['user_list']=array();
			foreach($openids as $val)
			{
				$arr['user_list'][]=array('openid'=>$val,'lang'=>'zh_CN');
			}
			$res=$WeiXin->getUsersInfo($openids);
			if($res!==false) {
				$res['errcode']=0;
				$res['errmsg']='ok';
			}
		}
	}
	else if($enews=='createMenu')
	{
		// 设置菜单 http://www.xxxxxx.com/api.php?operation=createMenu
		$input = file_get_contents("php://input");
		$input = '{
			"button":[
			{	
				"type":"view",
				"name":"菜单1",
				"url":"http://'.$_SERVER['HTTP_HOST'].'/webapp/index.php?t=3"
			},
			{	
				"type":"view",
				"name":"菜单2",
				"url":"http://'.$_SERVER['HTTP_HOST'].'/webapp/index.php?t=2"
			},
			{	
				"type":"view",
				"name":"菜单3",
				"url":"http://'.$_SERVER['HTTP_HOST'].'/webapp/index.php?t=1"
			}
			]
		 }';
		$data  = json_decode($input);
		echo '<pre>';
		print_r($data);
		$res=$WeiXin->createMenu($data);
	}
	else if($enews=='getQRCode')
	{
		// 生成二维码 http://www.xxxxxx.com/api.php?operation=getQRCode&scene_id=BindPhoneQR
		$scene_id=$_GET['scene_id'];//'BindPhoneQR'
		$res=$WeiXin->getQRCode($scene_id,2);
		if($res!==false) {
			$res['errcode']=0;
			$res['errmsg']='ok';
		}
	}
	else if($enews=='getJsSign')
	{
		// JS-SDK签名 http://www.xxxxxx.com/api.php?operation=getJsSign
		$url=$_GET['url']?$_GET['url']:'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$res=$WeiXin->getJsSign($url);
		if($res!==false) {
			$res['errcode']=0;
			$res['errmsg']='ok';
		}
	}
	else if($enews=='getJsConfig')
	{
		// JS-SDK签名+注册 http://www.xxxxxx.com/api.php?operation=getJsSign
		$url=$_GET['url']?$_GET['url']:'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$url=$_SERVER['HTTP_REFERER'];
		$res=$WeiXin->getJsConfig($url);
		if($res===false) {
			$res['errcode']=0;
			$res['errmsg']='ok';
		}
		else
		{
			echo $res;
			return;
		}
	}
	else if($enews=='GetVerifyCode') //获取验证码和提交绑定
	{
		$url='http://www.xxxxxx.com/POST_TEST.php';
		$res=$WeiXin->http_post($url,$_POST);
		if($res===false) $res='{"keyID":"404","states":404}';
		echo $res;
		exit;
	}
	else
	{
		$res=array('states'=>40011,'errmsg'=>'enews err('.$enews.')');
	}
	if($res===false) {
		$res=array('errcode'=>$WeiXin->errCode,'errmsg'=>$WeiXin->errMsg);
	}
	if($_SERVER['HTTP_HOST']=='wx.eduhz.com') //PHP版本处理
	{
		echo json_encode($res);
	} else {
		echo json_encode($res, JSON_UNESCAPED_UNICODE);
	}
}
else
{
	//微信通信认证
	$WeiXin->valid();
	//消息内容(array)
	$Data   = $WeiXin->getRev()->getRevData();
	//消息类型
	$Type   = $WeiXin->getRevType();
	$openid = $WeiXin->getRevFrom();
	switch($Type) {
		case Wechat::MSGTYPE_TEXT:
			//$WeiXin->text('<a href="http://'.$_SERVER['HTTP_HOST'].'/auth.php">点击绑定手机(api)</a>')->reply();
			break;
		case Wechat::MSGTYPE_EVENT:
			$Event = $WeiXin->getRevEvent();
			if($Event['event']==Wechat::EVENT_SCAN) //带参二维码
			{
				//$WeiXin->text('扫带参数二维码(key:'.$Event['key'].')')->reply();
				//exit;
				QRCode($Event['key'],$openid);
				//$QRID  = $WeiXin->getRevSceneId();
				if($Event['key']=='BindPhoneQR') //绑定URL
				{
					//$WeiXin->text('<a href="http://'.$_SERVER['HTTP_HOST'].'/auth.php">点击绑定手机(QR)</a>')->reply();
				}
				else if(strstr($Event['key'], 'bindUid_'))//家长绑定学生
				{
					
					$Event['key']=str_replace('bindUid_','',$Event['key']);
					$WeiXin->text('绑定学生(key:'.$Event['key'].')')->reply();
				}
				else
				{
					$WeiXin->text('扫带参数二维码(key:'.$Event['key'].')')->reply();
				}
			}
			else
			{
				//自动回复
				$text='欢迎使用';
				$WeiXin->text($text)->reply();
			}
			break;
		case Wechat::MSGTYPE_IMAGE:
			$WeiXin->text("hello, I'm wechat3")->reply();
			break;
		default:
			$WeiXin->text("hello, I'm wechat4")->reply();
	}

}
